﻿using System;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using RSERV.Web.Domain;

namespace RSERV.Web.DAO
{
    public static class TableInfo
    {
        public static List<String> getTableNames()
        {
            List<String> returnList = new List<String>();
            try
            {
                SqlConnection connection = new SqlConnection(InstanceSettings.connection);
                connection.Open();
                SqlCommand command = connection.CreateCommand();
                command.CommandTimeout = connection.ConnectionTimeout;
                command.CommandType = System.Data.CommandType.Text;
                command.CommandText = "SELECT name FROM sys.tables";
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    returnList.Add(reader["name"].ToString());
                }
                return returnList;

            }
            catch (Exception)
            {

                return returnList;
            }

        }

        public static List<MyData> getTable(String tableName)
        {
            try
            {
                SqlConnection connection = new SqlConnection(InstanceSettings.connection);
                connection.Open();
                SqlCommand command = connection.CreateCommand();
                command.CommandTimeout = connection.ConnectionTimeout;
                command.CommandType = System.Data.CommandType.Text;
                string cmd = "SELECT * FROM KathoPersons";
                command.CommandText = cmd;
                SqlDataReader reader = command.ExecuteReader();
                List<MyData> list = new List<MyData>();
                var Rownr = 0;
                while (reader.Read())
                {
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    list.Add( new MyData() { RowNr = Rownr, ColName= reader.GetName(i), value=reader[i].ToString()});
                }
                }
                return list;

            }
            catch (Exception)
            {

                return null;
            }

        }
    }
}